<?php

App::uses('CrudController', 'Controller');
App::uses('Test', 'Model');

class SchedulesController extends CrudController {

    public $_model = 'Schedule';

    public function graph() {
        $this->loadModel('Room');
        $this->loadModel('Test');
        $rooms = $this->Room->getEnabled();
        $tests = $this->Test->getEnabled();
        $appBosch = new stdClass();
        $appBosch->rooms = $rooms;
        $appBosch->tests = $tests;
        $this->set('appBosch', $appBosch);
        $this->set('title', __('Programación de examenes'));
        $this->set('description', __('Administración'));
    }

    public function inscription() {
        $appBosch = new stdClass();
        $this->set('title', __('Cursos'));
        $this->set('description', __('Inscripciónes'));
    }

    public function getMyTests() {
        $user = $this->Auth->user();
        $areaId = $user['area_id'];
        $userId = $user['id'];
        $schedules = $this->Schedule->getSchedulesByAreaIdAndUserId($areaId, $userId);
        $this->set(array('records' => $schedules, '_serialize' => 'records'));
        $this->viewClass = 'Json';
    }

    public function getMyTests2() {
        $user = $this->Auth->user();
        $areaId = $user['area_id'];
        $userId = $user['id'];
        $schedules = $this->Schedule->getSchedulesByAreaIdAndUserId2($areaId, $userId);
        $this->set(array('records' => $schedules, '_serialize' => 'records'));
        $this->viewClass = 'Json';
    }

    public function thirdParty() {
        $appBosch = new stdClass();
        $this->set('title', __('Cursos'));
        $this->set('description', __('Inscripción de terceros'));
    }

    /**
     * Devuelve los examenes disponibles para el usuario logueado. Es usada por
     *  la accion de registro a examanes de los usuarios.
     */
    public function getTestForUser() {
        $this->request->allowMethod('get');
        $this->loadModel('UserLine');
        $records = $this->UserLine->getSchedulesEnabledByLineByUser($this->Auth->user('id'));
        $this->set(array('records' => $records, '_serialize' => 'records'));
        $this->viewClass = 'Json';
    }

    public function getTestForArea() {
        $this->request->allowMethod('get');
        $m = $this->_model;
        $areaId = $this->request->query['filter']['filters'][0]['value'];
        $records = $this->$m->getEnabledByArea($areaId);
        $this->set(array('records' => $records, '_serialize' => 'records'));
        $this->viewClass = 'Json';
    }

    public function getInfoTest($scheduleId) {
        $this->request->allowMethod('get');
        $m = $this->_model;
        $records = $this->$m->getTestInfo($scheduleId);
        $this->set(array('records' => $records, '_serialize' => 'records'));
        $this->viewClass = 'Json';
    }

    public function getTestByDates() {
        $this->request->allowMethod('get');
        $m = $this->_model;
        $startD = $this->request->query['startD'];
        $endD = $this->request->query['endD'];
        $filterClosed = $this->request->query['filterClosed'];
        $records = $this->$m->getTestByDates($startD, $endD, $filterClosed);
        $this->set(array('records' => $records, '_serialize' => 'records'));
        $this->viewClass = 'Json';
    }

    protected function getRecords($param)
    {
        $m = $this->_model;
        $records = $this->$m->getEnabled($param);
        return $records;
    }

    protected function c($model)
    {
        $user = $this->Auth->user();
        $dt = new DateTime($model->start);
        return array(
            'test_id' => trim($model->test_id),
            'user_id' => $user['id'],
            'title' => trim($model->title),
            'description' => trim($model->description),
            'test_date' => $dt->format('Y-m-d H:i:s'),
        );
    }

    protected function u($model)
    {
        $dt = new DateTime($model->test_date);
        return array(
            'test_id' => trim($model->test_id),
            'user_name' => trim($model->user_name),
            'location' => trim($model->location),
            'test_date' => $dt->format('Y-m-d H:i:s'),
        );
    }
}